package org.ielts.action;

import static com.yifeng.struts2.ResultParameters.*;
import static com.yifeng.struts2.ResultConstants.*;
import static com.yifeng.struts2.ActionUtils.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.ielts.domain.base.Course;
import org.ielts.domain.base.SignCourse;
import org.ielts.domain.base.Student;
import org.ielts.domain.log.SignCourseLog;
import org.ielts.domain.security.User;
import org.ielts.service.ConsultantService;
import org.ielts.service.CourseService;
import org.ielts.service.SignCourseLogService;
import org.ielts.service.SignCourseService;
import org.ielts.service.StudentService;
import org.ielts.service.security.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import com.yifeng.ProfileContext;
import com.yifeng.modules.annotations.Operation;
import com.yifeng.profile.Profile;
import com.yifeng.utils.JsonResultBuilder;

public class SignCourseLogAction extends BaseAction {

	@Autowired
	private SignCourseLogService signCourseLogService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private UserService userService;

	@Operation(result = "JSON")
	public String create() {
		// 学员编号
		String id = form.readString("id");

		String stuId = form.readString("stuId");

		if (StringUtils.isNotBlank(stuId)) {
			studentService.changeStuId(id, stuId);
		}

		String courseId = form.readString("courseId");
		String contractId = form.readString("contractId");
		String cardId = form.readString("cardId");
		String title = form.readString("title");
		String lessonCard = form.readString("lessonCard");
		int times = valueReader.name("times").defaultValue(0).intValue();

		String consultantId = form.readString("consultantId");

		Date beginTime = valueReader.name("beginTime").dateValue("yyyy-MM-dd");
		Date endTime = valueReader.name("endTime").dateValue("yyyy-MM-dd");

		String book = form.readString("book");
		String note = form.readString("note");

		// Date signTime = valueReader.name("signTime").dateValue("yyyy-MM-dd");

		SignCourseLog clog = new SignCourseLog();
		clog.setEndTime(endTime);
		clog.setBeginTime(beginTime);
		clog.setTimes(times);
		clog.setLessonCard(lessonCard);
		clog.setTitle(title);
		clog.setCardId(cardId);
		clog.setContractId(contractId);

		Course course = new Course();
		course.setId(courseId);
		clog.setCourse(course);
		clog.setStudent(studentService.findOne(id));
		clog.setTerm(form.readString("term"));
		clog.setConsultant(userService.findUser(consultantId));
		User creator = (User) Profile.getInstance().getUser();

		clog.setCreator(creator);

		clog.setBook(book);

		clog.setNote(note);

		clog.setSignCourse(null);

		setJson(JsonResultBuilder.success(true).msg("提交成功").add("id", id).json());
		signCourseLogService.save(clog);
		return JSON;
	}

}
